/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package PurchaseOrder;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Mukesh
 */
public class ReceiveOrder extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            if (request.getParameter("reqFrom").equals("search")) {
                if (request.getParameter("orderNo").length() != 0) {
                    String id = request.getParameter("orderNo");
                    PurchaseOrderBean pob = PurchaseOrder.Query.viewSpecific(id);
                    request.setAttribute("pob", pob);
                    getServletContext().getRequestDispatcher("/Services/Inventory/Receive.jsp").forward(request, response);
                } else {
                    response.sendRedirect("/RIT/Services/Inventory/ReceiveOrder.jsp?status=0");
                }
            } else {
                if (request.getParameter("approvalNo").length() != 0) {
                    int approvalNo = Integer.parseInt(request.getParameter("approvalNo"));
                    int i = PurchaseOrder.Query.chkApprovalNo(approvalNo);
                    if (i != 0) {
                        ReceiveOrderBean rb = new ReceiveOrderBean();
                        
                        rb.setUser_id(Integer.parseInt(session.getAttribute("uid").toString()));
                        if (request.getParameter("return").length() != 0) {
                            rb.setReturnOutwards(Integer.parseInt(request.getParameter("return")));
                        } else {
                            rb.setReturnOutwards(0);
                        }
                        if (request.getParameter("tamnt").length() != 0) {
                            rb.setTotalAmount(Float.parseFloat(request.getParameter("tamnt")));
                        }
                        if (request.getParameter("orderId").length() != 0) {
                            rb.setPurchaseOrderId(Integer.parseInt(request.getParameter("orderId")));
                        }
                        int j = PurchaseOrder.Query.addReceiveOrder(rb);
                        if (j != 0) {
                            session.setAttribute("status", "Success");
                            session.setAttribute("msg", "Product Received Successfully.");
                            response.sendRedirect("/RIT/Services/Inventory/ReceiveOrder.jsp");
                        } else {
                            session.setAttribute("status", "Fail");
                            session.setAttribute("msg", "Check Order Details and try again.");
                            response.sendRedirect("/RIT/Services/Inventory/ReceiveOrder.jsp");
                        }

                    } else {
                        session.setAttribute("status", "Fail");
                        session.setAttribute("msg", "Approval No is Not Correct. <br>Contact to Admin");
                        response.sendRedirect("/RIT/Services/Inventory/ReceiveOrder.jsp");
                    }
                }

            }
        } catch (Exception e) {
            session.setAttribute("status", "Fail");
            session.setAttribute("msg", "Check Order Details and try again.(e)");
            response.sendRedirect("/RIT/Services/Inventory/ReceiveOrder.jsp");

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
